<template>
    <div class="updataAddress">
     <topNav :title="'新建'" :update="'保存'" @send="send"/>
     <addressDetail/>
    </div>
</template>

<script setup>

import  addressDetail  from "./components/addressDetail.vue";
import  topNav  from "./components/topNav.vue";
import { useRoute, useRouter } from "vue-router";
import { ref, watch,onUnmounted } from "vue";
import { post } from "../../utils/request.js";
import { Toast } from "vant";
import { setSession,getSession } from "../../utils/index.js";
import { mapMutations,mapState,mapActions,useStore } from "vuex";
const store=useStore()
const route = useRoute();
const router = useRouter();

console.log(route.params.judge);


const send=async ()=>{
     Toast.loading({
    message: "正在提交新地址，请稍后...",
    forbidClick: true,
    duration: 0,
  });
    let result
    let data=getSession('address')
    console.log(data);
    let timer=null
  try {
    result = await post("/address", data);  
  } catch (error) {
    console.log(error);
     timer=setTimeout(() => {
      Toast.clear();
      Toast.fail({message:'提交失败，请稍后再试', forbidClick: true,})
      clearTimeout(timer)
    }, 3000);
  }
 if (result.status == 200) {
  console.log(111111);
   Toast.clear()
      Toast.success({message:'提交成功，跳回原页面', forbidClick: true,})
   
   if (route.params.judge) {
    router.back()
   } else {
     router.push('/myAddress');
   }
     
   
 }
}
  onUnmounted(() => {
   Toast.clear();
      
    });
</script>

<style lang="scss" scoped>
.updataAddress{
    width: 100%;
    height: 100vh;
    background-color: #f1f1f1;
}
</style>